<?php

/**
 * This is the model class for table "Client".
 *
 * The followings are the available columns in table 'Client':
 * @property integer $id
 * @property string $civilite
 * @property string $nom
 * @property string $prenom
 * @property string $adresse
 * @property string $codePostal
 * @property string $ville
 * @property string $pays
 * @property string $dateDeNaissance
 * @property string $tel
 * @property string $valid
 * @property integer $id_user
 *
 * The followings are the available model relations:
 * @property Artiste $artiste
 * @property User $user
 * @property Commande[] $commandes
 * @property Livraison[] $livraisons
 */
class Client extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Client the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Client';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
		array('civilite, nom, prenom, adresse, codePostal, ville, pays, dateDeNaissance, tel, valid, id_user', 'required'),
		array('civilite', 'length', 'max'=>4),
		array('codePostal', 'length', 'max'=>5, 'min'=>5),
		array('codePostal', 'numerical', 'integerOnly'=>true),
		array('dateDeNaissance', 'type', 'type' => 'date', 'message' => 'Le format de la date de naissance n\'est pas valide.', 'dateFormat' => 'dd/MM/yyyy'),
		array('nom, prenom', 'length', 'max'=>50),
		array('adresse', 'length', 'max'=>500),
		array('ville', 'length', 'max'=>100),
		array('tel', 'length', 'max'=>20),
		array('valid', 'length', 'max'=>1),
		// The following rule is used by search().
		// Please remove those attributes that should not be searched.
		array('id, civilite, nom, prenom, adresse, codePostal, ville, pays, dateDeNaissance, tel, id_user', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'artiste' => array(self::HAS_ONE, 'Artiste', 'id_client'),
			'user' => array(self::BELONGS_TO, 'User', 'id_user'),
			'commandes' => array(self::HAS_MANY, 'Commande', 'id_client'),
			'livraisons' => array(self::HAS_MANY, 'Livraison', 'id_client'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'civilite' => 'Civilite',
			'nom' => 'Nom',
			'prenom' => 'Prenom',
			'adresse' => 'Adresse',
			'codePostal' => 'Code Postal',
			'ville' => 'Ville',
			'pays' => 'Pays',
			'dateDeNaissance' => 'Date De Naissance',
			'tel' => 'Tel',
			'valid' => 'Valid',
			'id_user' => 'Id User',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('civilite',$this->civilite,true);
		$criteria->compare('nom',$this->nom,true);
		$criteria->compare('prenom',$this->prenom,true);
		$criteria->compare('adresse',$this->adresse,true);
		$criteria->compare('codePostal',$this->codePostal,true);
		$criteria->compare('ville',$this->ville,true);
		$criteria->compare('pays',$this->pays,true);
		$criteria->compare('dateDeNaissance',$this->dateDeNaissance,true);
		$criteria->compare('tel',$this->tel,true);
		$criteria->compare('valid',$this->valid,true);
		$criteria->compare('id_user',$this->id_user);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

	public static function getClientFromUser() {
		return Client::model()->findByAttributes(array('id_user'=>Yii::app()->user->id));
	}

	public static function getTroisDerniersClients () {
		$criteria = new CDbCriteria;
		$criteria->limit = 3;
		return Client::model()->findAll($criteria);
	}
}